<?php

namespace App\Admin\Controllers\Service;

use App\Models\Open\MicroApp;
use App\Models\Open\SysAccount;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Auth;
use App\Admin\Controllers\BaseAdminController;

class MicroAppController extends BaseAdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $adminId = Admin::user()->getKey();
        if($adminId == 1) {
            $model = MicroApp::with(['adminUser', 'sysAccount', 'apiInfos']);
        } else {
            $model = MicroApp::where('admin_user_id', $adminId)->with(['adminUser', 'sysAccount', 'apiInfos']);
        }
        return Grid::make($model, function (Grid $grid) {
            $grid->enableDialogCreate();
            $grid->disableEditButton();
            $grid->showQuickEditButton('编辑');

            $grid->column('name');
            $grid->column('aliasname');
            $grid->column('adminUser.username', admin_trans_field('admin_username'));
            $grid->column('sysAccount.name', admin_trans_field('sys_account_name'));
            $grid->apiInfos()->pluck('name')->label()->width('300px');
            $grid->column('appid')->copyable();
            $grid->column('appsecret')->copyable();
//            $grid->column('whitelist_ips')->width('150px');
//            $grid->column('whitelist_domains')->width('150px');
            $grid->column('updated_at')->sortable();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new MicroApp(), function (Show $show) {
            $show->field('id');
            $show->field('admin_user_id');
            $show->field('name');
            $show->field('description');
            $show->field('sys_account_id');
            $show->field('appid');
            $show->field('appsecret');
            $show->field('access_token');
            $show->field('expire_time');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $adminId = Admin::user()->getKey();
        return Form::make(MicroApp::where('admin_user_id', $adminId)->with(['adminUser', 'sysAccount']), function (Form $form) {
            $adminId = Admin::user()->getKey();
            $form->hidden('admin_user_id')->value($adminId);
            $form->text('name');
            $form->text('aliasname');
            $form->textarea('description');
            if($form->isEditing()) {
                $form->display('adminUser.username', admin_trans_field('admin_username'));
                $form->display('appid');
                $form->text('appsecret');
            } else if($form->isCreating()) {
                $form->hidden('appid');
                $form->hidden('appsecret');
                $form->select('sys_account_id', admin_trans_field('sys_account_name'))
                    ->options(function () {
                        return SysAccount::all()->pluck('name', 'id');
                    });
            }
            $form->textarea('whitelist_ips');
            $form->textarea('whitelist_domains');
        })->saving(function (Form $form) {
            if($form->isCreating()) {
                $form->appid = md5('appid_'.time().'_'.mt_rand(0, 1000));
                $form->appsecret = md5('secret_'.time().'_'.mt_rand(0, 1000));
            } else if($form->isEditing()) {
                if($form->model()->get('appsecret') != $form->appsecret) {
                    MicroApp::find($form->getKey())->update(['access_token'=>null, 'expire_time'=>null]);
                }
            }
        });
    }
}
